package com.snow.useviewdemo.base;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;

import androidx.databinding.ViewDataBinding;

import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.snow.baselibrary.permission.RequestPermissions;
import com.snow.common.tool.utils.PermissionsUtils;

import java.io.File;
import java.util.ArrayList;

/**
 * author : zyt
 * e-mail : 632105276@qq.com
 * date   : 2019-01-08
 * desc   :基础类，被继承用的
 */
public abstract class BaseAddPicActivity<T extends ViewDataBinding> extends BaseActivity<T> {
    protected ArrayList<String> imagesPath;//多选图片地址
    protected int maxSelectNum = 1;//默认值选择一张图
    private boolean isCutPicture = false;//是否剪切图片
    //图片的裁剪比例
    private int cropRatioX = 3;
    private int cropRatioY = 4;
    protected ArrayList<LocalMedia> alreadySelectList;//已经选择的图片带入
    protected boolean isWithSelectList;//是否带着选择的图片

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        imagesPath = new ArrayList<>();
        super.onCreate(savedInstanceState);
    }

    public Activity getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onSupportNavigateUp() {
        onBackPressed();
        return true;
    }

    /**
     * 设置裁剪比例===代表需要裁剪
     *
     * @param x
     * @param y
     */
    protected void setCropRatio(int x, int y) {
        isCutPicture = true;
        this.cropRatioX = x;
        this.cropRatioY = y;
    }

    /**
     * 选择相册或者拍照
     */
    public void needAlbumAndCamera() {
        RequestPermissions.with(this)
                .permissions(new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE})
                .onGranted(strings -> {

                })
                .start();

//        new BottomDialog(this, 0, type)
//                .setBottomDialogContent("从相册选择", "拍照", (position) -> {
//                            switch (position) {
//                                case 2:
//                                    addMultiPicture();
//                                    break;
//                                case 1:
//                                    addCameraPicture();
//                                    break;
//                            }
//                        }
//                );
    }

    /**
     * 调用开源库的相机拍照/录视频
     */
    protected void cameraOfAll() {
        PictureSelector.create(this)
                .openCamera(PictureMimeType.ofAll())
                .enableCrop(isCutPicture)
                .showCropGrid(false)
                .withAspectRatio(cropRatioX, cropRatioY)
                .isDragFrame(true)
                .videoQuality(1)// 视频录制质量 0 or 1 int
                .forResult(PictureConfig.CHOOSE_REQUEST);
    }

    /**
     * 调用开源库相册 视频，图片
     *
     * @param maxSecond 单位为秒s
     */
    protected void albumOfAll(int maxSecond) {
        PictureSelector.create(this)
                .openGallery(PictureMimeType.ofAll())
                .imageSpanCount(4)// 每行显示个数 int
                .isCamera(false)// 是否显示拍照按钮 true or false
                .videoMaxSecond(maxSecond)// 显示多少秒以内的视频or音频也可适用 int
                .videoMinSecond(5)
                .selectionMode(PictureConfig.SINGLE)// 多选 or 单选 PictureConfig.MULTIPLE or PictureConfig.SINGLE
                .forResult(PictureConfig.CHOOSE_REQUEST);//结果回调onActivityResult code
    }

    /**
     * 调用开源库的相机拍照
     */
    protected void addCameraPicture() {
        PictureSelector.create(this)
                .openCamera(PictureMimeType.ofImage())
                .enableCrop(isCutPicture)
                .showCropGrid(true)
                .withAspectRatio(cropRatioX, cropRatioY)
                .isDragFrame(true)
                .forResult(PictureConfig.CHOOSE_REQUEST);
    }

    /**
     * 相机录制
     *
     * @param maxTime 录制的时间
     */
    protected void cameraVideo(int maxTime) {
        if (!PermissionsUtils.getInstance().verifyOnePermissions(this, Manifest.permission.CAMERA)) {
            return;
        }
        PictureSelector.create(this)
                .openCamera(PictureMimeType.ofVideo())
                .recordVideoSecond(maxTime)//视频秒数录制 默认60s int
                .videoQuality(1)// 视频录制质量 0 or 1 int
                .forResult(PictureConfig.CHOOSE_REQUEST);
    }

    /**
     * 视频相册
     *
     * @param maxSecond 单位为秒s
     */
    protected void albumVideo(int maxSecond) {
        PictureSelector.create(this)
                .openGallery(PictureMimeType.ofVideo())
                .imageSpanCount(4)// 每行显示个数 int
                .isCamera(false)// 是否显示拍照按钮 true or false
                .videoMaxSecond(maxSecond)// 显示多少秒以内的视频or音频也可适用 int
                .videoMinSecond(5)
                .selectionMode(PictureConfig.SINGLE)// 多选 or 单选 PictureConfig.MULTIPLE or PictureConfig.SINGLE
                .forResult(PictureConfig.CHOOSE_REQUEST);//结果回调onActivityResult code
    }

    /**
     * 添加多张图片
     * 添加多张或者单张 修改maxSelectNum值即可
     */
    protected void addMultiPicture() {
        int selectMode;
        if (maxSelectNum > 1) {
            selectMode = PictureConfig.MULTIPLE;
        } else {
            selectMode = PictureConfig.SINGLE;
        }
        if (!isWithSelectList && alreadySelectList != null) {//不需要带入数据
            alreadySelectList.clear();
        }
        PictureSelector.create(this)
                .openGallery(PictureMimeType.ofImage())
                .maxSelectNum(maxSelectNum)// 最大图片选择数量 int
                .imageSpanCount(4)// 每行显示个数 int
                .compress(true)
                .enableCrop(isCutPicture)
                .showCropGrid(true)
                .withAspectRatio(cropRatioX, cropRatioY)
                .isDragFrame(true)
                .selectionMedia(alreadySelectList)
                .isCamera(false)// 是否显示拍照按钮 true or false
                .selectionMode(selectMode)// 多选 or 单选 PictureConfig.MULTIPLE or PictureConfig.SINGLE
                .forResult(PictureConfig.CHOOSE_REQUEST);//结果回调onActivityResult code
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //多选图片回调
        if (resultCode == RESULT_OK) {
            if (requestCode == PictureConfig.CHOOSE_REQUEST) {
                alreadySelectList = (ArrayList<LocalMedia>) PictureSelector.obtainMultipleResult(data);
                if (alreadySelectList != null) {
                    for (LocalMedia media : alreadySelectList) {
                        if (media.isCut()) {//文件是否剪切过
                            imagesPath.add(media.getCutPath());
                        } else {
                            imagesPath.add(media.getPath());
                        }
                    }
                }
            }
        }
    }
}
